#cs ----------------------------------------------------------------------------

	AutoIt Version: 3.3.10.2
	Author:         Nothgrin

	Script Function:
	Castle Clash Bot for Dungeons and Arena

#ce ----------------------------------------------------------------------------

;Shards
;4558 @ 3:28AM 6/20/14
;4596 @ 8:31AM
;4718 @ 12:31PM after 30 for druid and HBMs
;4766 @ 9:40PM
;4859 @ 11:41PM after HBMs and 50 donation
;4913 @ 5:53AM 6/21/14
;5451 @ 9:34PM 6/22/14
;5859 @ 7:45AM 6/24/14
;5881 @ 11:22AM 6/24/14
;6105 @ 8:48PM 6/24/14 After HBMs
;6289 @ 9:38AM 6/25/14 after HBMs
;6327 @ 9:52PM
;421 @ 6:24PM 6/28/14
;831 @ 10:40AM 6/29/14
;585 @ 12:40AM 7/1/14
;1336 @ 4:48PM 7/2/14

;HB
;77901	@ 8:57AM 6/20/14
;62472 @ 9:44PM 6/22/14


Opt("MouseCoordMode", 0)
Opt("PixelCoordMode", 0)

Opt("GUIOnEventMode", 1)

#include <ColorConstants.au3>
#include <ProgressConstants.au3>

;Global Variables
$MouseClickVariance = 20

$MouseClickSpeed = 8

$Delay = 7000

$Here = False

$MinutesToMilli = 60000

$TimeoutTime = 1

$TimerTolerance = 2

$TimeSinceLastMapReset = 0

$UpdateSpeed = 500

Local $ApplicationHandle = 0

Local $ActiveWindowHandle = 0

Local $DungeonTimerWindowHandle = 0
Local $ArenaTimerWindowHandle = 0
Local $HBMTimerWindowHandle = 0
Local $HTTimerWindowHandle = 0

Local $DungeonTimerHandle = TimerInit()
Local $ArenaTimerHandle = TimerInit()
Local $HBMTimerHandle = TimerInit()
Local $HTTimerHandle = TimerInit()
Local $WastedTimerHandle = TimerInit()

$WastedTime = 0

Local $DungeonProgressTimer = 0
Local $ArenaProgressTimer = 0
Local $HBMProgressTimer = 0
Local $HTProgressTimer = 0

Local $DungeonProgressColor[3] = [0x00, 0x80, 0xff]
Local $ArenaProgressColor[3] = [0xff, 0xf2, 0x00]

Local $ExitTextX[] = [775, 775, 784, 785, 785, 794, 794, 800, 815, 815, 815, 823, 826, 830, 826]
Local $ExitTextY[] = [799, 821, 799, 810, 820, 805, 820, 814, 798, 807, 821, 806, 801, 820, 813]

Const $White = 0xFFFFFF

;--------------- Griffon Hiring -----------------
$GriffonCount = 16
$OrniCount = 4
$HireVariance = 5

$HireButtonX = 985
$HireButtonY = 779

$TroopSlot1X = 424
$TroopSlot1Y = 720

$TroopSlotDelta = 245

$XButtonX = 1304
$XButtonY = 97

$ArmyCamp1X = 455
$ArmyCamp1Y = 486

;2
;721
;676

;3
;950
;183

;4
;1178
;347

Local $ArmyCampsX[] = [0, 266, 460, 723]
Local $ArmyCampsY[] = [0, 190, -332, -139]

;--------------- Arena Variables -----------------
$ArenaBaseX = 160
$ArenaBaseY = 440

$ArenaEnterX = 879
$ArenaEnterY = 789

$ArenaTargetHero1X = 417
$ArenaTargetHero1Y = 709

$ArenaTargetHeroDelta = 193

$ArenaChallengeX = 1167
$ArenaChallengeY = 496

$ArenaHeroSlot1X = 83
$ArenaHeroSlot1Y = 795

$ArenaHeroSlotDelta = 150

$ArenaBottomLaneX = 296
$ArenaBottomLaneY = 618

$ArenaBottomPinPointX = 355
$ArenaBottomPinPointY = 625

$ArenaStartX = 804
$ArenaStartY = 284

$ArenaBattleCloseX = 1516
$ArenaBattleCloseY = 76

Local $ArenaHeroSlots[] = [1, 2, 3, 4, 5, 9]

$ArenaTimer = 30

$ArenaLength = 1

$TimeSinceLastArena = 180000

;------------- Dungeon Variables ---------------
$BattleButtonX = 71
$BattleButtonY = 810

$DungeonButtonX = 269
$DungeonButtonY = 746

$DungeonBackButtonX = 299
$DungeonBackButtonY = 79

$OrdinaryDungeonX = 451
$OrdinaryDungeonY = 80

$ExpertDungeonX = 663
$ExpertDungeonY = 80

$DungeonSlot1X = 481
$DungeonSlot1Y = 566

$Dungeon31X = 1024
$Dungeon31Y = 272

$Dungeon31Length = 60;seconds

$Dungeon32X = 1080
$Dungeon32Y = 512

$Dungeon32Length = 90;seconds

$Dungeon32DownX = 1020
$Dungeon32DownY = 440

$Dungeon32UpX = 380

$Dungeon41X = 929
$Dungeon41Y = 304

$Dungeon41Length = 70;seconds

$Dungeon51X = 1140
$Dungeon51Y = 280

$Dungeon51Length = 80;seconds

$Dungeon811X = 979
$Dungeon811Y = 327

$Dungeon811Length = 150;seconds

$Dungeon815X = 1257
$Dungeon815Y = 362

$Dungeon815Length = 150;seconds

$DungeonLevelDelta = 396

$AttackButtonX = 1170
$AttackButtonY = 772

$DungeonHero1X = 86
$DungeonHero1Y = 815

$DungeonHeroDelta = 150

$SpellOffset = 0

$SpellSlot1X = 63
$SpellSlot1Y = 643

$SpellSlotDelta = 119

$ExitButtonX = 805
$ExitButtonY = 811

$DungeonSwap = 0

$DungeonDelay = 300

$DungeonTimer = 20

$TimeSinceLastDungeon = 0

$CancelGemBuyoutX = 1138
$CancelGemBuyoutY = 273

Local $DungeonHeroSlots[] = [1, 2, 3, 4, 5, 6]
Local $DungeonXPHeroSlots[] = [1, 2, 3, 4, 6] ;Update $XPHeroCount when this is updated

Const $XPHeroCount = 5
Const $TotalHeroCount = 6

;----------------- Spells -------------------
$RelicHallX = 214
$RelicHallY = 403

$MagicButtonX = 880
$MagicButtonY = 786

$Spell1X = 435
$Spell1Y = 557

$SpellDelta = 322

Local $Spells[] = [2, 3, 5, 8]

;-------------- Recruitment -----------------
$MeleeL1 = 12
$MeleeL2 = 71 ;true
$MeleeL3 = 246

$RangedL1 = 19
$RangedL2 = 98 ;true
$RangedL3 = 390

$MagicL1 = 26
$MagicL2 = 186 ;true
$MagicL3 = 624

$DestructiveL1 = 39
$DestructiveL2 = 279 ;true
$DestructiveL3 = 1008

$OneStar = 2010
$TwoStar = 4020
$ThreeStar = 6019

;----------------- HBM -------------------
$HBMTimer = 180 * 6

$HBMLength = 420 ;seconds

$TimeSinceLastHBM = 300000

$CaveX = 266
$CaveY = 79

$HBM_Challenge2X = 692
$HBM_Challenge2Y = 88

$HBMSideX = 368
$HBMSideY = 79

$HBM_ROW1 = 373
$HBM_ROW2 = 563

$HBM_COLUMN1 = 666
$HBM_COLUMN2 = 814
$HBM_COLUMN3 = 961
$HBM_COLUMN4 = 1107
$HBM_COLUMN5 = 1254

$HBM_HX = $HBM_COLUMN3
$HBM_HY = $HBM_ROW2

$HBM_LX = $HBM_COLUMN2
$HBM_LY = $HBM_ROW1

$HBM_MX = $HBM_COLUMN3
$HBM_MY = $HBM_ROW1

$HBMStartX = 961
$HBMStartY = 713

;------------------ HT--------------------
$HTTimer = 360 * 6

$HTLength = 185 ;seconds

$TimeSinceLastHT = 600000

$HTSideX = 173
$HTSideY = 112

$HT_Row1 = 365

$HT_L1X = 389
$HT_L1Y = $HT_Row1

$HT_L2X = 551
$HT_L2Y = $HT_Row1

$HTStartX = 804
$HTStartY = 715

$HTExitButtonX = 804
$HTExitButtonY = 702

;----------------- Lost Realm -------------
$LR_EntryX = 1449
$LR_EntryY = 115

$Row1 = 322
$Row2 = 474
$Row3 = 578

Local $LR_MinesX[] = [  520,   804,  944,  1014,   732,   520]
Local $LR_MinesY[] = [$Row3, $Row3,$Row2, $Row1, $Row1, $Row2]

$LR_MineCount = 5

$LR_CollectX = 990
$LR_CollectY = 794

;---------------- Hotkeys -------------------
HotKeySet("^{ENTER}", "end")
HotKeySet("^{`}", "ResetMap")
HotKeySet("^{1}", "Dungeon51NoTroops")
HotKeySet("^{2}", "ArenaBotWithWastedTime")
;HotKeySet("^{3}", "HireOrnithopters")
HotKeySet("^{3}", "HireGriffons")
;HotKeySet("^{4}", "BuySpells")
HotKeySet("^{4}", "Dungeon814")
HotKeySet("^{5}", "Pause20")
;HotKeySet("^{5}", "HireOrnithopters")
HotKeySet("^{6}", "SpellTranslate3")
;HotKeySet("^{7}", "Dungeon31")

HotKeySet("^{9}", "Here")
HotKeySet("^{0}", "NotHere")

ActivateWindow()

$TimerWindowHandle = GUICreate("Dungeon & Arena Timers", 1200, 150, 250, 885, -1, -1, $ApplicationHandle)

$DungeonProgressTimer = GUICtrlCreateProgress(10, 10, 385, 35)
GUICtrlSetColor($DungeonProgressTimer, $DungeonProgressColor)
$ArenaProgressTimer = GUICtrlCreateProgress(400, 10, 390, 35)
GUICtrlSetColor($ArenaProgressTimer, $ArenaProgressColor)
$HBMProgressTimer = GUICtrlCreateProgress(10, 80, 250, 35)
$HTProgressTimer = GUICtrlCreateProgress(270, 80, 250, 35)

Local $DungeonResetButton = GUICtrlCreateButton("RESET", 10, 50)
GUICtrlSetOnEvent($DungeonResetButton, "DungeonTimerReset")
Local $DungeonTypeButton = GUICtrlCreateButton("8-14", 325, 50, 70)
GUICtrlSetOnEvent($DungeonTypeButton, "DungeonTypeChange")

Local $ArenaResetButton = GUICtrlCreateButton("RESET", 400, 50)
GUICtrlSetOnEvent($ArenaResetButton, "ArenaTimerReset")

Local $HBMResetButton = GUICtrlCreateButton("RESET", 10, 120)
GUICtrlSetOnEvent($HBMResetButton, "HBMTimerReset")
Local $HBMAddTimeButton = GUICtrlCreateButton(">", 57, 120)
GUICtrlSetOnEvent($HBMAddTimeButton, "HBMAddTime")

Local $HTResetButton = GUICtrlCreateButton("RESET", 270, 120)
GUICtrlSetOnEvent($HTResetButton, "HTTimerReset")
Local $HTAddTimeButton = GUICtrlCreateButton(">", 317, 120)
GUICtrlSetOnEvent($HTAddTimeButton, "HTAddTime")

GUICtrlCreateLabel("Dungeon Timer", 175, 55)
GUICtrlCreateLabel("Arena Timer", 575, 55)
GUICtrlCreateLabel("HBM Timer", 100, 125)
GUICtrlCreateLabel("HT Timer", 400, 125)

Local $RecruitmentInput = GUICtrlCreateInput("2508", 740, 80, 50)
Local $RecruitmentCalculateButton = GUICtrlCreateButton("0", 650, 110, 140)

GUISetState(@SW_SHOW)

TimerDiff($DungeonTimerHandle)
TimerDiff($ArenaTimerHandle)
TimerDiff($HBMTimerHandle)
TimerDiff($HTTimerHandle)
TimerDiff($WastedTimerHandle)

While True ;Main Loop

	If $Here Then

		;Keep from going inactive
		Sleep($UpdateSpeed)

		UpdateTimersAndGUI()

		#cs

			If ($TimeSinceLastMapReset > (($DungeonTimer * $MinutesToMilli) + $TimerTolerance)) Then
			ResetMap()
			EndIf

		#ce

	Else

		Sleep($UpdateSpeed)

		UpdateTimersAndGUI()

		If ($TimeSinceLastDungeon > (($DungeonTimer * $MinutesToMilli) + $TimerTolerance)) Then

			GetActiveWindow()

			;Beep(500, 1000) Crashes system

			If ($DungeonSwap = 0) Then
				;Dungeon51NoTroops()
				Dungeon814()
				DungeonTypeChange()
			Else
				;Dungeon51NoTroops()
				;Dungeon811()
				Dungeon814()
				DungeonTypeChange()
			EndIf

			SetActiveWindow()

		EndIf

		If ($TimeSinceLastArena > (($ArenaTimer * $MinutesToMilli) + $TimerTolerance)) Then

			#cs

				GetActiveWindow()

				;Beep(500, 1000) Crashes system

				;Auto Arena Here
				ArenaAutoSelect()
				ArenaBot()

				SetActiveWindow()

			#ce

			GetActiveWindow()

			LostRealmCollector()

			SetActiveWindow()

		EndIf

		If ($TimeSinceLastHBM > (($HBMTimer * $MinutesToMilli) + $TimerTolerance)) Then

			GetActiveWindow()

			HBMRun()

			SetActiveWindow()

		EndIf

		If ($TimeSinceLastHT > (($HTTimer * $MinutesToMilli) + $TimerTolerance)) Then

			GetActiveWindow()

			HTRun()

			SetActiveWindow()

		EndIf

		#cs
			Sleep(($ArenaTimer-$DungeonTimer) * $MinutesToMilli)
			;Auto Arena Here

			Sleep(($ArenaTimer-$DungeonTimer) * $MinutesToMilli)
			Dungeon32()

			Sleep($DungeonTimer * $MinutesToMilli)
			Dungeon32()
			;Auto Arena Here

		#ce

	EndIf

WEnd

Func HireGriffons()

	ActivateWindow()

	ResetMap()

	MouseClick("left", $ArmyCamp1X, $ArmyCamp1Y, 1, $MouseClickSpeed)
	MouseClick("left")
	MouseClick("left")

	Local $MousePos = MouseGetPos()

	For $i = 0 To 3 Step 1

		MouseClick("left", $MousePos[0] + $ArmyCampsX[$i], $MousePos[1] + $ArmyCampsY[$i], 1, $MouseClickSpeed)
		Sleep(15)
		MouseClick("left", $MousePos[0] + $ArmyCampsX[$i], $MousePos[1] + $ArmyCampsY[$i], 1, $MouseClickSpeed)
		Sleep(15)
		MouseClick("left", $MousePos[0] + $ArmyCampsX[$i], $MousePos[1] + $ArmyCampsY[$i], 1, $MouseClickSpeed)

		Sleep(1000)

		MouseClick("left", $HireButtonX, $HireButtonY + RandVariance(), 1, $MouseClickSpeed)

		Sleep($Delay / 2)

		MouseMove($TroopSlot1X + ($TroopSlotDelta * 3) + RandVariance(), $TroopSlot1Y + RandVariance(), $MouseClickSpeed)
		MouseDown("left")
		MouseMove($TroopSlot1X + RandVariance(), $TroopSlot1Y + RandVariance(), $MouseClickSpeed)
		MouseUp("left")
		MouseClick("left", $TroopSlot1X + ($TroopSlotDelta * 3) + RandVariance(), $TroopSlot1Y + RandVariance(), $GriffonCount + Random(0, $HireVariance, 1), $MouseClickSpeed)

		MouseClick("left", $XButtonX, $XButtonY, 1, $MouseClickSpeed)

		Sleep(1000)

	Next

	MouseClick("left")

EndFunc   ;==>HireGriffons

Func HireOrnithopters()

	ActivateWindow()

	ResetMap()

	MouseClick("left", $ArmyCamp1X, $ArmyCamp1Y, 1, $MouseClickSpeed)
	MouseClick("left")
	MouseClick("left")

	Local $MousePos = MouseGetPos()

	For $i = 0 To 3 Step 1

		MouseClick("left", $MousePos[0] + $ArmyCampsX[$i], $MousePos[1] + $ArmyCampsY[$i], 1, $MouseClickSpeed)
		Sleep(15)
		MouseClick("left", $MousePos[0] + $ArmyCampsX[$i], $MousePos[1] + $ArmyCampsY[$i], 1, $MouseClickSpeed)
		Sleep(15)
		MouseClick("left", $MousePos[0] + $ArmyCampsX[$i], $MousePos[1] + $ArmyCampsY[$i], 1, $MouseClickSpeed)

		Sleep(1000)

		MouseClick("left", $HireButtonX, $HireButtonY + RandVariance(), 1, $MouseClickSpeed)

		Sleep($Delay / 2)

		MouseMove($TroopSlot1X + ($TroopSlotDelta * 3) + RandVariance(), $TroopSlot1Y + RandVariance(), $MouseClickSpeed)
		MouseDown("left")
		MouseMove($TroopSlot1X + RandVariance(), $TroopSlot1Y + RandVariance(), $MouseClickSpeed)
		MouseUp("left")
		MouseMove($TroopSlot1X + ($TroopSlotDelta * 3) + RandVariance(), $TroopSlot1Y + RandVariance(), $MouseClickSpeed)
		MouseDown("left")
		MouseMove($TroopSlot1X + RandVariance(), $TroopSlot1Y + RandVariance(), $MouseClickSpeed)
		MouseUp("left")
		MouseMove($TroopSlot1X + ($TroopSlotDelta * 3) + RandVariance(), $TroopSlot1Y + RandVariance(), $MouseClickSpeed)
		MouseDown("left")
		MouseMove($TroopSlot1X + RandVariance(), $TroopSlot1Y + RandVariance(), $MouseClickSpeed)
		MouseUp("left")

		#cs
			MouseMove($TroopSlot1X + ($TroopSlotDelta * 3) + RandVariance(), $TroopSlot1Y + RandVariance(), $MouseClickSpeed)
			MouseDown("left")
			MouseMove($TroopSlot1X + ($TroopSlotDelta * 2) + RandVariance(), $TroopSlot1Y + RandVariance(), $MouseClickSpeed)
			MouseUp("left")
		#ce

		MouseClick("left", $TroopSlot1X + ($TroopSlotDelta * 3) + RandVariance(), $TroopSlot1Y + RandVariance(), $OrniCount + Random(0, $HireVariance, 1), $MouseClickSpeed)

		MouseClick("left", $XButtonX, $XButtonY, 1, $MouseClickSpeed)

		Sleep(1000)

	Next

	MouseClick("left")

EndFunc   ;==>HireOrnithopters

Func ArenaAutoSelect()

	WastedTimerStart()

	ActivateWindow()

	ResetMap()

	MouseClick("left", $ArenaBaseX, $ArenaBaseY, 1, $MouseClickSpeed)

	Sleep($DungeonDelay)

	MouseClick("left", $ArenaEnterX + RandVariance(), $ArenaEnterY + RandVariance(), 1, $MouseClickSpeed)

	Sleep($Delay * 2)

	#cs

		$ArenaTargetHero1X=	417
		$ArenaTargetHero1Y=	709

		$ArenaTargetHeroDelta=	193

	#ce

	;Pixel Search Here

	;if false

	;Random target

	MouseClick("left", $ArenaTargetHero1X + (Random(0, 4, 1) * $ArenaTargetHeroDelta) + RandVariance(), $ArenaTargetHero1Y + RandVariance(), $MouseClickSpeed)

EndFunc   ;==>ArenaAutoSelect

Func ArenaBot()

	ActivateWindow()

	MouseClick("left")
	MouseClick("left")
	MouseClick("left")

	Sleep(200)

	MouseClick("left", $ArenaChallengeX + RandVariance(), $ArenaChallengeY + RandVariance(), 1, $MouseClickSpeed)

	Sleep($Delay / 2)

	WastedTimerEnd()

	$ArenaTimerHandle = TimerInit()

	For $i = 0 To 5 Step 1
		MouseClick("left", $ArenaHeroSlot1X + RandVariance() + (($ArenaHeroSlots[$i] - 1) * $ArenaHeroSlotDelta), $ArenaHeroSlot1Y + RandVariance(), 1, $MouseClickSpeed)
		MouseClick("left", $ArenaBottomPinPointX, $ArenaBottomPinPointY, 1, $MouseClickSpeed)
	Next

	MouseClick("left", $ArenaStartX + RandVariance(), $ArenaStartY + RandVariance(), 1, $MouseClickSpeed)

	Sleep($ArenaLength * $MinutesToMilli)

	ActivateWindow()

	CloseWindows()

	MouseClick("left", $ExitButtonX + RandVariance(), $ExitButtonY + RandVariance(), 1, $MouseClickSpeed)

	Sleep($Delay)

	MouseClick("left", $XButtonX, $XButtonY, 1, $MouseClickSpeed)

	Sleep(1500)

	MouseClick("left")

	ArenaTimerReset()

EndFunc   ;==>ArenaBot

Func ArenaBotWithWastedTime()

	WastedTimerStart()

	ArenaBot()

EndFunc   ;==>ArenaBotWithWastedTime

Func DungeonMapReset()

	ActivateWindow()

	CloseWindows()

	Sleep($DungeonDelay)

	MouseClick("left", $BattleButtonX + RandVariance(), $BattleButtonY + RandVariance(), 3, $MouseClickSpeed)

	Sleep($DungeonDelay)

	MouseClick("left", $DungeonButtonX + RandVariance(), $DungeonButtonY + RandVariance(), 1, $MouseClickSpeed)

	Sleep($Delay)

	MouseClick("left", $DungeonBackButtonX, $DungeonBackButtonY, 1, $MouseClickSpeed)

	Sleep($Delay)

	MouseClick("left", $OrdinaryDungeonX, $OrdinaryDungeonY, 1, $MouseClickSpeed)

	Sleep($Delay / 2)

	MouseClick("left", $ExpertDungeonX, $ExpertDungeonY, 1, $MouseClickSpeed)

	Sleep($Delay / 2)

	MouseClick("left", $OrdinaryDungeonX, $OrdinaryDungeonY, 1, $MouseClickSpeed)

	Sleep($Delay / 2)

	#cs

		MouseMove(329, 329)
		MouseDown("left")
		MouseMove(1294, 329, 1)
		Sleep(1000)
		MouseUp("left")

		MouseMove(329, 329)
		MouseDown("left")
		MouseMove(1294, 329, 1)
		Sleep(1000)
		MouseUp("left")

	#ce

EndFunc   ;==>DungeonMapReset

Func DungeonLevelTranslate2()

	MouseMove(1278, 560)
	MouseDown("left")
	MouseMove(495, 560, 10)
	Sleep(1000)
	MouseUp("left")

EndFunc   ;==>DungeonLevelTranslate2

Func Dungeon814()

	WastedTimerStart()

	DungeonMapReset()

	Sleep(1000)

	DungeonLevelTranslate2()
	DungeonLevelTranslate2()
	DungeonLevelTranslate2()

	Sleep(1000)

	MouseClick("left", 1144 + RandVariance(), 524 + RandVariance(), 1, $MouseClickSpeed)

	Sleep($DungeonDelay)

	MouseMove(1319, 432)
	MouseDown("left")
	MouseMove(282, 432, $MouseClickSpeed)
	Sleep(1000)
	MouseUp("left")

	;1257,362
	MouseClick("left", $Dungeon815X + RandVariance(), $Dungeon815Y + RandVariance(), 1, $MouseClickSpeed)

	Sleep($DungeonDelay)

	MouseClick("left", $AttackButtonX + RandVariance(), $AttackButtonY + RandVariance(), 1, $MouseClickSpeed)

	Sleep($Delay * 3)

	MouseMove($Dungeon32UpX, $Dungeon32DownY, $MouseClickSpeed)

	Send("^{- 10}")

	Sleep(1500)

	Send("^{- 10}")

	Sleep(1500)

	;258, 416 Drop Location
	;Druid
	MouseClick("left", $DungeonHero1X + RandVariance() + ($DungeonHeroDelta * ($DungeonHeroSlots[0] - 1)), $DungeonHero1Y + RandVariance(), 1, $MouseClickSpeed)

	MouseClick("left", 270, 416, $TotalHeroCount, $MouseClickSpeed)

	WastedTimerEnd()

	$DungeonTimerHandle = TimerInit()

	;Exit
	Sleep($Dungeon815Length * 1000)
	ActivateWindow()

	CloseWindows()

	MouseClick("left", $ExitButtonX + RandVariance(), $ExitButtonY + RandVariance(), 1, $MouseClickSpeed)

	Sleep($DungeonDelay)

	DungeonTimerReset()

EndFunc   ;==>Dungeon814

Func Dungeon811()

	WastedTimerStart()

	DungeonMapReset()

	Sleep(1000)

	DungeonLevelTranslate2()
	DungeonLevelTranslate2()
	DungeonLevelTranslate2()

	Sleep(1000)

	MouseClick("left", 1144 + RandVariance(), 524 + RandVariance(), 1, $MouseClickSpeed)

	Sleep($DungeonDelay)

	MouseMove(1249, 432)
	MouseDown("left")
	MouseMove(355, 432, $MouseClickSpeed)
	Sleep(1000)
	MouseUp("left")

	;D 8-11 Position
	MouseClick("left", $Dungeon811X + RandVariance(), $Dungeon811Y + RandVariance(), 1, $MouseClickSpeed)

	Sleep($DungeonDelay)

	MouseClick("left", $AttackButtonX + RandVariance(), $AttackButtonY + RandVariance(), 1, $MouseClickSpeed)

	Sleep($Delay)

	MouseMove($Dungeon32UpX, $Dungeon32DownY, $MouseClickSpeed)

	Send("^{- 8}")

	;Reset Dungeon Map
	MouseMove(1300, 457)
	MouseDown("left")
	MouseMove(1300, 156, $MouseClickSpeed)
	Sleep(1000)
	MouseUp("left")

	;Spell 4
	MouseClick("left", $SpellSlot1X + RandVariance() + ($SpellSlotDelta * 3), $SpellSlot1Y + RandVariance(), 1, $MouseClickSpeed)

	MouseClick("left", 761, 589, 1, $MouseClickSpeed)

	;Druid
	MouseClick("left", $DungeonHero1X + RandVariance() + ($DungeonHeroDelta * ($DungeonHeroSlots[0] - 1)), $DungeonHero1Y + RandVariance(), 1, $MouseClickSpeed)

	MouseClick("left", 761, 589, $TotalHeroCount, $MouseClickSpeed)

	#cs

		MouseClick("left", 761, 615, 1, $MouseClickSpeed)

		MouseClick("left", 761, 589, $TotalHeroCount-3, 0)

		MouseClick("left", 761, 615, 2, 0)

	#ce

	WastedTimerEnd()

	$DungeonTimerHandle = TimerInit()

	Sleep(1400)

	;Spell 3
	MouseClick("left", $SpellSlot1X + RandVariance() + ($SpellSlotDelta * 2), $SpellSlot1Y + RandVariance(), 1, $MouseClickSpeed)

	MouseClick("left", 761, 540, 1, $MouseClickSpeed)

	Sleep(12000)

	;Click Ornis
	MouseClick("left", $DungeonHero1X + RandVariance() + ($DungeonHeroDelta * $TotalHeroCount), $DungeonHero1Y + RandVariance(), 1, $MouseClickSpeed)

	MouseClick("left", 556, 223, $OrniCount * 4, $MouseClickSpeed)

	;Exit
	Sleep($Dungeon811Length * 1000)
	ActivateWindow()

	CloseWindows()

	MouseClick("left", $ExitButtonX + RandVariance(), $ExitButtonY + RandVariance(), 1, $MouseClickSpeed)

	Sleep($DungeonDelay)
	BuySpells()

	HireOrnithopters()

	DungeonTimerReset()

EndFunc   ;==>Dungeon811

Func Dungeon51NoTroops()

	WastedTimerStart()

	DungeonMapReset()

	Sleep(1000)

	DungeonLevelTranslate2()
	DungeonLevelTranslate2(); Level 5

	MouseClick("left", $DungeonSlot1X + RandVariance(), $DungeonSlot1Y + RandVariance(), 1, $MouseClickSpeed)

	Sleep($DungeonDelay)

	MouseMove(1056, 210)
	MouseDown("left")
	MouseMove(330, 210, $MouseClickSpeed)
	Sleep(1000)
	MouseUp("left")

	;1140, 280
	MouseClick("left", $Dungeon51X + RandVariance(), $Dungeon51Y + RandVariance(), 1, $MouseClickSpeed)

	Sleep($DungeonDelay)

	MouseClick("left", $AttackButtonX + RandVariance(), $AttackButtonY + RandVariance(), 1, $MouseClickSpeed)

	Sleep($Delay)

	MouseMove($Dungeon32UpX, $Dungeon32DownY, $MouseClickSpeed)

	Send("^{- 8}")
	Sleep(100)
	Send("^{- 8}")

	;Druid
	MouseClick("left", $DungeonHero1X + RandVariance() + ($DungeonHeroDelta * ($DungeonHeroSlots[0] - 1)), $DungeonHero1Y + RandVariance(), 1, $MouseClickSpeed)

	MouseClick("left", 1149, 331, $TotalHeroCount, $MouseClickSpeed)

	WastedTimerEnd()

	$DungeonTimerHandle = TimerInit()

	Sleep(250)

	;Spell 2
	;MouseClick("left", $SpellSlot1X + RandVariance() + $SpellSlotDelta, $SpellSlot1Y + RandVariance(), 1, $MouseClickSpeed)

	;MouseClick("left", 1149, 331, 1, $MouseClickSpeed)

	;Exit
	Sleep($Dungeon51Length * 1000)
	ActivateWindow()

	CloseWindows()

	MouseClick("left", $ExitButtonX + RandVariance(), $ExitButtonY + RandVariance(), 1, $MouseClickSpeed)

	;Sleep($DungeonDelay)
	;BuySpells()

	DungeonTimerReset()

EndFunc   ;==>Dungeon51NoTroops

Func Dungeon51()

	DungeonMapReset()

	DungeonLevelTranslate2()
	DungeonLevelTranslate2(); Level 5

	MouseClick("left", $DungeonSlot1X + RandVariance(), $DungeonSlot1Y + RandVariance(), 1, $MouseClickSpeed)

	Sleep($DungeonDelay)

	MouseMove(1056, 210)
	MouseDown("left")
	MouseMove(330, 210, $MouseClickSpeed)
	Sleep(1000)
	MouseUp("left")

	;1140, 280
	MouseClick("left", $Dungeon51X + RandVariance(), $Dungeon51Y + RandVariance(), 1, $MouseClickSpeed)

	Sleep($DungeonDelay)

	MouseClick("left", $AttackButtonX + RandVariance(), $AttackButtonY + RandVariance(), 1, $MouseClickSpeed)

	Sleep($Delay)

	MouseMove($Dungeon32UpX, $Dungeon32DownY, $MouseClickSpeed)

	Send("^{- 8}")

	;Click Ornis
	MouseClick("left", $DungeonHero1X + RandVariance() + ($DungeonHeroDelta * $TotalHeroCount), $DungeonHero1Y + RandVariance(), 1, $MouseClickSpeed)

	;1149, 331 drop point in 5-1
	MouseClick("left", 1149, 331, $GriffonCount * 4, $MouseClickSpeed)

	$DungeonTimerHandle = TimerInit()

	Sleep(250)

	;Druid
	MouseClick("left", $DungeonHero1X + RandVariance() + ($DungeonHeroDelta * ($DungeonHeroSlots[0] - 1)), $DungeonHero1Y + RandVariance(), 1, $MouseClickSpeed)

	MouseClick("left", 1149, 331, $TotalHeroCount, $MouseClickSpeed)

	;Spell 2
	MouseClick("left", $SpellSlot1X + RandVariance() + $SpellSlotDelta, $SpellSlot1Y + RandVariance(), 1, $MouseClickSpeed)

	MouseClick("left", 1149, 331, 1, $MouseClickSpeed)

	;Exit
	Sleep($Dungeon51Length * 1000)
	ActivateWindow()

	CloseWindows()

	MouseClick("left", $ExitButtonX + RandVariance(), $ExitButtonY + RandVariance(), 1, $MouseClickSpeed)

	Sleep($DungeonDelay)
	BuySpells()

	HireGriffons()

	DungeonTimerReset()

EndFunc   ;==>Dungeon51

Func Dungeon41()

	DungeonMapReset()

	DungeonLevelTranslate2()

	Sleep($DungeonDelay + 500)

	MouseClick("left", $DungeonSlot1X + $DungeonLevelDelta + RandVariance(), $DungeonSlot1Y + RandVariance(), 1, $MouseClickSpeed)

	Sleep($DungeonDelay + 500)

	MouseMove(1215, 461)
	MouseDown("left")
	MouseMove(686, 438, $MouseClickSpeed)
	Sleep(1000)
	MouseUp("left")

	MouseClick("left", $Dungeon41X + RandVariance(), $Dungeon41Y + RandVariance(), 1, $MouseClickSpeed)

	Sleep($DungeonDelay)

	MouseClick("left", $AttackButtonX + RandVariance(), $AttackButtonY + RandVariance(), 1, $MouseClickSpeed)

	Sleep($Delay)

	MouseMove($Dungeon32UpX, $Dungeon32DownY, $MouseClickSpeed)

	Sleep(500)

	Send("^{- 8}")

	;1217, 676
	MouseClick("left", $DungeonHero1X + RandVariance() + ($DungeonHeroDelta * ($DungeonHeroSlots[0] - 1)), $DungeonHero1Y + RandVariance(), 1, $MouseClickSpeed)

	MouseClick("left", 1217, 676, $TotalHeroCount, $MouseClickSpeed)

	$DungeonTimerHandle = TimerInit()

	;Spell 2
	;MouseClick("left", $SpellSlot1X + RandVariance() + $SpellSlotDelta, $SpellSlot1Y + RandVariance(), 1, $MouseClickSpeed)

	;MouseClick("left", 1217, 676, 1, $MouseClickSpeed)

	;Exit
	Sleep($Dungeon41Length * 1000)
	ActivateWindow()

	CloseWindows()

	MouseClick("left", $ExitButtonX + RandVariance(), $ExitButtonY + RandVariance(), 1, $MouseClickSpeed)

	Sleep($DungeonDelay)

	DungeonTimerReset()

EndFunc   ;==>Dungeon41

Func Dungeon32()

	DungeonMapReset()

	DungeonLevelTranslate2()

	MouseClick("left", $DungeonSlot1X + RandVariance(), $DungeonSlot1Y + RandVariance(), 1, $MouseClickSpeed)

	Sleep($DungeonDelay)

	;Screen Translate
	MouseMove($Dungeon32DownX, $Dungeon32DownY)
	MouseDown("left")
	MouseMove($Dungeon32UpX, $Dungeon32DownY, $MouseClickSpeed)
	Sleep(1000)
	MouseUp("left")

	MouseClick("left", $Dungeon32X + RandVariance(), $Dungeon32Y + RandVariance(), 1, $MouseClickSpeed)

	Sleep($DungeonDelay)

	MouseClick("left", $AttackButtonX + RandVariance(), $AttackButtonY + RandVariance(), 1, $MouseClickSpeed)

	Sleep($Delay)

	MouseMove($Dungeon32UpX, $Dungeon32DownY, $MouseClickSpeed)

	#cs
		;Zoom all the way out
		Send("{CTRLDOWN}")
		MouseWheel("down", 10)
		Send("{CTRLUP}")
	#ce

	Send("^{- 8}")

	;Druid
	MouseClick("left", $DungeonHero1X + RandVariance() + ($DungeonHeroDelta * ($DungeonHeroSlots[0] - 1)), $DungeonHero1Y + RandVariance(), 1, $MouseClickSpeed)

	MouseClick("left", 435, 485, 1, $MouseClickSpeed)

	Sleep(250)

	$DungeonTimerHandle = TimerInit()

	MouseClick("left", 435, 485, 4, $MouseClickSpeed)

	;Spell 2
	MouseClick("left", $SpellSlot1X + RandVariance() + $SpellSlotDelta, $SpellSlot1Y + RandVariance(), 1, $MouseClickSpeed)

	MouseClick("left", 435, 485, 1, $MouseClickSpeed)

	;Exit
	Sleep($Dungeon32Length * 1000)
	ActivateWindow()

	CloseWindows()

	MouseClick("left", $ExitButtonX + RandVariance(), $ExitButtonY + RandVariance(), 1, $MouseClickSpeed)

	Sleep($DungeonDelay)
	BuySpells()

	DungeonTimerReset()

EndFunc   ;==>Dungeon32

Func Dungeon31()

	DungeonMapReset()

	Sleep(500)

	DungeonLevelTranslate2()

	Sleep($DungeonDelay)

	MouseClick("left", $DungeonSlot1X + RandVariance(), $DungeonSlot1Y + RandVariance(), 1, $MouseClickSpeed)

	Sleep($DungeonDelay)

	MouseClick("left", $Dungeon31X + RandVariance(), $Dungeon31Y + RandVariance(), 1, $MouseClickSpeed)

	Sleep($DungeonDelay)

	MouseClick("left", $AttackButtonX + RandVariance(), $AttackButtonY + RandVariance(), 1, $MouseClickSpeed)

	Sleep($Delay)

	Send("^{- 8}")

	#cs
		;Screen Translate
		MouseMove(596, 268)
		MouseDown("left")
		MouseMove(915, 525, $MouseClickSpeed)
		Sleep(1000)
		MouseUp("left")
	#ce


	#cs
		;Druid
		MouseClick("left", $DungeonHero1X + RandVariance() + ($DungeonHeroDelta * ($DungeonHeroSlots[0] - 1)), $DungeonHero1Y + RandVariance(), 1, $MouseClickSpeed)

		MouseClick("left", 484, 317, 1, $MouseClickSpeed)

		Sleep(250)

		$DungeonTimerHandle = TimerInit()

		MouseClick("left", 484, 317, 4, $MouseClickSpeed)
	#ce

	XPHeroes(484, 317)

	;Spell 2
	;MouseClick("left", $SpellSlot1X + RandVariance() + $SpellSlotDelta, $SpellSlot1Y + RandVariance(), 1, $MouseClickSpeed)

	;MouseClick("left", 484, 317, 1, $MouseClickSpeed)

	;Exit
	ActivateWindow()
	Local $i = 0
	Do

		Sleep(1000)
		$i += 1

	Until (($i = $Dungeon31Length) Or CheckExit())

	ActivateWindow()

	CloseWindows()

	MouseClick("left", $ExitButtonX + RandVariance(), $ExitButtonY + RandVariance(), 1, $MouseClickSpeed)

	Sleep($DungeonDelay)
	;BuySpells()

	DungeonTimerReset()

EndFunc   ;==>Dungeon31

Func XPHeroes($Xposition = 400, $Yposition = 400)

	#cs
		;Druid
		MouseClick("left", $DungeonHero1X + RandVariance() + ($DungeonHeroDelta * ($DungeonHeroSlots[0] - 1)), $DungeonHero1Y + RandVariance(), 1, $MouseClickSpeed)

		MouseClick("left", 484, 317, 1, $MouseClickSpeed)

		Sleep(250)

		$DungeonTimerHandle = TimerInit()

		MouseClick("left", 484, 317, 4, $MouseClickSpeed)
	#ce

	For $i = 0 To ($XPHeroCount - 1) Step 1

		If ($i = 0) Then

			MouseClick("left", $DungeonHero1X + RandVariance() + ($DungeonHeroDelta * ($DungeonXPHeroSlots[$i] - 1)), $DungeonHero1Y + RandVariance(), 1, $MouseClickSpeed)

			MouseClick("left", $Xposition, $Yposition, 1, $MouseClickSpeed)

			$DungeonTimerHandle = TimerInit()

			Sleep(250)

		Else

			MouseClick("left", $DungeonHero1X + RandVariance() + ($DungeonHeroDelta * ($DungeonXPHeroSlots[$i] - 1)), $DungeonHero1Y + RandVariance(), 1, 1)

			MouseClick("left", $Xposition, $Yposition, 1, $MouseClickSpeed)

		EndIf

	Next


EndFunc   ;==>XPHeroes

Func HBMRun()

	ActivateWindow()

	For $i = 0 To 5 Step 1

		ResetMap()

		MouseClick("left", $CaveX, $CaveY, 1, $MouseClickSpeed)

		MouseClick("left", $HBMSideX, $HBMSideY, 1, $MouseClickSpeed)

		Sleep(1500)

		MouseClick("left", $HBM_Challenge2X, $HBM_Challenge2Y, 1, $MouseClickSpeed)

		Sleep(1500)

		MouseClick("left", $HBM_MX, $HBM_MY, 1, $MouseClickSpeed)

		MouseClick("left", $HBMStartX, $HBMStartY, 1, $MouseClickSpeed)

		Sleep($HBMLength * 1000)

		MouseClick("left", $ExitButtonX + RandVariance(), $ExitButtonY + RandVariance(), 1, $MouseClickSpeed)

		Sleep($Delay)

	Next

	HBMTimerReset()

	;HireOrnithopters()

EndFunc   ;==>HBMRun

Func HBMAddTime()

	$TimeSinceLastHBM += (30 * $MinutesToMilli)

EndFunc   ;==>HBMAddTime

Func HTRun()

	ActivateWindow()

	For $i = 0 To 5 Step 1

		ResetMap()

		MouseClick("left", $CaveX, $CaveY, 1, $MouseClickSpeed)

		MouseClick("left", $HTSideX, $HTSideY, 1, $MouseClickSpeed)

		Sleep(1500)

		MouseClick("left", $HT_L1X, $HT_L1Y, 1, $MouseClickSpeed)

		MouseClick("left", $HTStartX, $HTStartY, 1, $MouseClickSpeed)

		Sleep($HTLength * 1000)

		MouseClick("left", $HTExitButtonX, $HTExitButtonY, 1, $MouseClickSpeed)

		Sleep($Delay)

	Next

	HTTimerReset()

	;HireOrnithopters()

EndFunc   ;==>HTRun

Func LostRealmCollector()

	ActivateWindow()

	ResetMap()

	Sleep(1000)

	MouseClick("left", $LR_EntryX, $LR_EntryY, 1, $MouseClickSpeed)

	Sleep($Delay * 2)

	MouseMove($CancelGemBuyoutX, $CancelGemBuyoutY, $MouseClickSpeed)

	Send("^{- 10}")

	Sleep(1500)

	Send("^{- 10}")

	For $i = 0 To ($LR_MineCount - 1) Step 1

		MouseClick("left", $LR_MinesX[$i], $LR_MinesY[$i], 1, $MouseClickSpeed)

		Sleep(200)

		MouseClick("left", $LR_CollectX, $LR_CollectY, 1, $MouseClickSpeed)

		Sleep(1000)

	Next

	CloseWindows()

	ArenaTimerReset() ;Removed once new timer is set up for Lost Realm collection

EndFunc   ;==>LostRealmCollector

Func HTAddTime()

	$TimeSinceLastHT += (30 * $MinutesToMilli)

EndFunc   ;==>HTAddTime

Func ResetMap()

	ActivateWindow()

	CloseWindows()

	MouseMove(1300, 300)
	MouseClick("left")

	#cs
		;Zoom all the way out
		Send("{CTRLDOWN}")
		MouseWheel("down", 10)
		Send("{CTRLUP}")

	#ce

	Send("^{- 8}")

	;scroll to the very bottom of the screen
	MouseWheel("down", 10)
	Sleep(1000)

	;scroll up so arena alter is next to chat icon
	MouseMove(1300, 300)
	MouseDown("left")
	Sleep(150)
	MouseMove(1300, 475)
	MouseUp("left")
	Sleep(150)

	$TimeSinceLastMapReset = 0

EndFunc   ;==>ResetMap

Func SpellReset()

	MouseMove(300, 612)
	MouseDown("left")
	MouseMove(1250, 612, 1)
	Sleep(1000)
	MouseUp("left")

	MouseMove(300, 612)
	MouseDown("left")
	MouseMove(1250, 612, 1)
	Sleep(1000)
	MouseUp("left")

	$SpellOffset = 0

EndFunc   ;==>SpellReset

Func SpellTranslate3()

	MouseMove(1278, 470)
	MouseDown("left")
	MouseMove(314, 470, 10)
	Sleep(1250)
	MouseUp("left")

EndFunc   ;==>SpellTranslate3

Func BuySpells()

	;ActivateWindow()

	ResetMap()

	MouseClick("left", $RelicHallX + RandVariance() / 2, $RelicHallY + RandVariance() / 2, 1, $MouseClickSpeed)

	Sleep(200)

	;MouseClick("left", $MagicButtonX + RandVariance(), $MagicButtonY + RandVariance(), 1, $MouseClickSpeed)
	MouseClick("left", $HireButtonX, $HireButtonY + RandVariance(), 1, $MouseClickSpeed)

	Sleep(250)

	SpellReset()

	Sleep(250)

	;Hacked code but should work - Will not work if spells are more than 3 apart...
	For $i = 0 To 3 Step 1

		If ($Spells[$i] > (($SpellOffset + 1) * 3)) Then

			SpellTranslate3()

		EndIf

		MouseClick("left", $Spell1X + RandVariance() + ($SpellDelta * Mod($Spells[$i] - 1, 3)), $Spell1Y + RandVariance(), 1, $MouseClickSpeed)

		Sleep(100)

	Next

	#cs

		For $i = 0 To 3 Step 1

		if($Spells[$i] <= (($SpellOffset + 1) * 3)) Then

		MouseClick("left", $Spell1X + RandVariance() + ($SpellDelta * Mod($Spells[$i]-1, 3)), $Spell1Y + RandVariance(), 1, $MouseClickSpeed)

		Sleep(5000)

		Else

		SpellTranslate3()

		EndIf

		Sleep(100)

		Next

	#ce

	MouseClick("left", $XButtonX, $XButtonY, 1, $MouseClickSpeed)

	Sleep(250)

	MouseClick("left", $XButtonX, $XButtonY, 1, $MouseClickSpeed)

	Sleep(1500)

	MouseClick("left")

EndFunc   ;==>BuySpells

Func DungeonTimerReset()

	$TimeSinceLastDungeon -= $DungeonTimer * $MinutesToMilli

	If ($TimeSinceLastDungeon < 0) Then

		$TimeSinceLastDungeon = 0

	EndIf

	$TimeSinceLastDungeon += $WastedTime

	$WastedTime = 0

EndFunc   ;==>DungeonTimerReset

Func ArenaTimerReset()

	$TimeSinceLastArena -= $ArenaTimer * $MinutesToMilli

	If ($TimeSinceLastArena < 0) Then

		$TimeSinceLastArena = 0

	EndIf

	$TimeSinceLastArena += $WastedTime

	$WastedTime = 0

EndFunc   ;==>ArenaTimerReset

Func HBMTimerReset()

	$TimeSinceLastHBM -= $HBMTimer * $MinutesToMilli

	If ($TimeSinceLastHBM < 0) Then

		$TimeSinceLastHBM = 0

	EndIf

EndFunc   ;==>HBMTimerReset

Func HTTimerReset()

	$TimeSinceLastHT -= $HTTimer * $MinutesToMilli

	If ($TimeSinceLastHT < 0) Then

		$TimeSinceLastHT = 0

	EndIf

EndFunc   ;==>HTTimerReset

Func UpdateTimersAndGUI()

	$TimerDelta = TimerDiff($DungeonTimerHandle)
	$TimeSinceLastDungeon += $TimerDelta

	$TimerDelta = TimerDiff($ArenaTimerHandle)
	$TimeSinceLastArena += $TimerDelta

	$TimerDelta = TimerDiff($HBMTimerHandle)
	$TimeSinceLastHBM += $TimerDelta

	$TimerDelta = TimerDiff($HTTimerHandle)
	$TimeSinceLastHT += $TimerDelta

	$DungeonTimerHandle = TimerInit()
	$ArenaTimerHandle = TimerInit()
	$HBMTimerHandle = TimerInit()
	$HTTimerHandle = TimerInit()

	;---------- Dungeon -----------
	GUICtrlSetData($DungeonProgressTimer, ($TimeSinceLastDungeon / ($DungeonTimer * $MinutesToMilli)) * 100)

	If ($TimeSinceLastDungeon < $MinutesToMilli) Then

		GUICtrlSetData($DungeonResetButton, "0:" & Int($TimeSinceLastDungeon / 1000))

	Else

		GUICtrlSetData($DungeonResetButton, Int($TimeSinceLastDungeon / $MinutesToMilli) & ":" & Mod(Int($TimeSinceLastDungeon / 1000), 60))

	EndIf

	;---------- Arena -----------
	GUICtrlSetData($ArenaProgressTimer, ($TimeSinceLastArena / ($ArenaTimer * $MinutesToMilli)) * 100)

	If ($TimeSinceLastArena < $MinutesToMilli) Then

		GUICtrlSetData($ArenaResetButton, "0:" & Int($TimeSinceLastArena / 1000))

	Else

		GUICtrlSetData($ArenaResetButton, Int($TimeSinceLastArena / $MinutesToMilli) & ":" & Mod(Int($TimeSinceLastArena / 1000), 60))

	EndIf

	;---------- HBM -----------
	GUICtrlSetData($HBMProgressTimer, ($TimeSinceLastHBM / ($HBMTimer * $MinutesToMilli)) * 100)

	If ($TimeSinceLastHBM < $MinutesToMilli) Then

		GUICtrlSetData($HBMResetButton, "0:" & Int($TimeSinceLastHBM / 1000))

	Else

		GUICtrlSetData($HBMResetButton, Int($TimeSinceLastHBM / $MinutesToMilli) & ":" & Mod(Int($TimeSinceLastHBM / 1000), 60))

	EndIf

	;---------- HT -----------
	GUICtrlSetData($HTProgressTimer, ($TimeSinceLastHT / ($HTTimer * $MinutesToMilli)) * 100)

	If ($TimeSinceLastHT < $MinutesToMilli) Then

		GUICtrlSetData($HTResetButton, "0:" & Int($TimeSinceLastHT / 1000))

	Else

		GUICtrlSetData($HTResetButton, Int($TimeSinceLastHT / $MinutesToMilli) & ":" & Mod(Int($TimeSinceLastHT / 1000), 60))

	EndIf

EndFunc   ;==>UpdateTimersAndGUI

Func DungeonTypeChange()

	$DungeonSwap = $DungeonSwap ? 0 : 1

	If ($DungeonSwap = 0) Then

		GUICtrlSetData($DungeonTypeButton, "8-14")

	Else

		GUICtrlSetData($DungeonTypeButton, "8-14")

	EndIf

EndFunc   ;==>DungeonTypeChange

Func end()
	Exit
EndFunc   ;==>end

Func _WinWaitActivate($title, $text, $timeout = 0)
	$ApplicationHandle = WinWait($title, $text, $timeout)
	If Not WinActive($title, $text) Then WinActivate($title, $text)
	WinWaitActive($title, $text, $timeout)
EndFunc   ;==>_WinWaitActivate

Func ActivateWindow()

	_WinWaitActivate("BlueStacks App Player", "")

	Sleep(250)

EndFunc   ;==>ActivateWindow

Func RandVariance()

	Return (Random(0, $MouseClickVariance * 2, 1) - $MouseClickVariance)

EndFunc   ;==>RandVariance

Func CheckExit()

	;ActivateWindow()

	For $i = 0 To 14 Step 1

		If (Not (Hex(PixelGetColor($ExitTextX[$i], $ExitTextY[$i], $ApplicationHandle)) = $White)) Then
			Return False
		EndIf

	Next

	Sleep(150)

	MouseClick("left", $ExitTextX[5], $ExitTextY[5], 1, $MouseClickSpeed)

	Return True

EndFunc   ;==>CheckExit

Func CloseWindows()

	MouseClick("left", $CancelGemBuyoutX, $CancelGemBuyoutY, 3, $MouseClickSpeed)

	Sleep(150)

	MouseClick("left", $ArenaBattleCloseX, $ArenaBattleCloseY, 3, $MouseClickSpeed)

	Sleep(150)

	MouseClick("left", $XButtonX, $XButtonY, 3, $MouseClickSpeed)

	Sleep(1500)

	MouseClick("left", $XButtonX, $XButtonY, 3, $MouseClickSpeed)

	Sleep(2000)

EndFunc   ;==>CloseWindows

Func Here()
	$Here = True
	MsgBox(0, "Here", "HERE!", $TimeoutTime)
EndFunc   ;==>Here

Func NotHere()
	$Here = False
	MsgBox(0, "Not Here", "BE GONE!", $TimeoutTime)
EndFunc   ;==>NotHere

Func WastedTimerStart()

	$WastedTimerHandle = TimerInit()

	$WastedTime = 0

EndFunc   ;==>WastedTimerStart

Func WastedTimerEnd()

	$WastedTime = TimerDiff($WastedTimerHandle)

EndFunc   ;==>WastedTimerEnd

Func Pause20()

	Sleep(20000)

	ActivateWindow()

EndFunc   ;==>Pause20

Func GetActiveWindow()

	$ActiveWindowHandle = WinGetHandle("[ACTIVE]")

EndFunc   ;==>GetActiveWindow

Func SetActiveWindow()

	_WinWaitActivate($ActiveWindowHandle, "")

EndFunc   ;==>SetActiveWindow
